<?php

namespace app\common\model\hfztcy;

use app\common\model\BaseModel;
use app\common\model\LibAdmin;
use app\common\model\LibUser;
use common\Common;
use think\Db;
use think\Exception;
use think\Validate;

//公共函数

/**
 * 订单
 * @name LibOrder.php
 * @package hfztcy
 * @category model
 * @link http://www.chanekeji.com
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2022-06-17
 */
class LibOrder extends BaseModel
{
    protected $model;
    protected $table = 'hfztcy_order';

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = db($this->table);
        parent::__construct();
    }

    /**
     * 添加表单信息
     */
    public function addOrder($condition)
    {
        try {
            $result = $this->model->insertGetId($condition);
            if (true == $result) {
                return Common::errorArray(0, "添加成功", $result);
            } else {
                return Common::errorArray(1, "添加失败", $result);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }


    /**
     * 查询单个表单信息
     * @param $condition
     * @param array $result
     */
    public function findOrder($condition = '', $field = '')
    {
        try {
            $reslut = $this->model->where($condition)->field($field)->find();
            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 查询多个表单信息
     * @param $condition
     * @param $limit
     * @param $field
     * @param $sort
     * @param array $result
     */
    public function findAllOrder($condition = '', $limit = '', $field = '', $sort = 'add_time desc')
    {
        try {
            $reslut = $this->model->where($condition)->order($sort)->limit($limit)->field($field)->select();
            if (true == $reslut) {
                return Common::errorArray(0, "查询成功", $reslut);
            } else {
                return Common::errorArray(1, "查询为空", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 更新表单信息
     * @param $condition
     * @param $userInfo
     * @param array $result
     */
    public function updateOrder($condition, $userInfo)
    {
        try {
            $reslut = $this->model->where($condition)->update($userInfo);
            if ($reslut >= 0) {
                return Common::errorArray(0, "更新成功", $reslut);
            } else {
                return Common::errorArray(1, "更新失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 删除表单信息
     * @param $condition
     * @param array $result
     */
    public function deleteAllOrder($ids)
    {
        $condition = array('id' => array('in', $ids));
        try {
            $reslut = $this->model->where($condition)->delete();;
            if (true == $reslut) {
                return Common::errorArray(0, "删除成功", $reslut);
            } else {
                return Common::errorArray(1, "删除失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 删除表单信息
     * @param $condition
     * @param array $result
     */
    public function deleteOrder($ids)
    {
        $condition = array('id' => $ids);
        try {
            $reslut = $this->model->where($condition)->delete();;
            if (true == $reslut) {
                return Common::errorArray(0, "删除成功", $reslut);
            } else {
                return Common::errorArray(1, "删除失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 字段值加1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function increaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->model->where($condition)->setInc($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 字段值减1
     * @param $condition
     * @param string $field
     * @param int $optval
     * @param array $reslut
     */
    public function decreaseField($condition, $field, $optval = 1)
    {
        try {
            $reslut = $this->model->where($condition)->setDec($field, $optval);;
            if (true == $reslut) {
                return Common::errorArray(0, "修改成功", $reslut);
            } else {
                return Common::errorArray(1, "修改失败", $reslut);
            }
        } catch (Exception $ex) {
            $this->errorLog(__CLASS__, __FUNCTION__, $ex);
            return Common::errorArray(1, "数据库操作失败" . $ex->getMessage(), $ex);
        }
    }

    /**
     * 分页获取数据表单信息
     */
    public function pagingOrderByStoreVcr($page, $whereStr)
    {
        $page['pageIndex'] ? $pageIndex = $page['pageIndex'] : $pageIndex = 1;
        $page['pageSize'] ? $pageSize = $page['pageSize'] : $pageSize = 10;

        $m = ($pageIndex - 1) * $pageSize;
        $n = $pageSize;

        $where = 'id IS NOT NULL';
        if ($whereStr) {
            $where = "{$whereStr}";
        }

        $querySQL = <<< SQL
SELECT * 
FROM (
    SELECT a.*,
           b.is_pick_up,b.pick_up_complete_time,
           c.username,c.is_vip,c.is_vip_video,c.province,c.city,c.area,c.vip_open_time,
           d.head_img_url,d.nick_name
    FROM `{$this->table}` `a` 
        LEFT JOIN `hfztcy_order_vip_auth` `b` 
            ON `a`.`id`=`b`.`order_id` 
        LEFT JOIN `hfztcy_user` `c` 
            ON `a`.`user_id`=`c`.`user_id` 
        LEFT JOIN `base_user` `d` 
            ON `a`.`user_id`=`d`.`id` 
    WHERE `type` = 2
) AS a
WHERE {$where} 
ORDER BY a.id DESC 
SQL;

        $sql = $querySQL . " LIMIT {$m}, {$n}";
        //echo $sql;die();
        $sqlAll = $querySQL;

        $result['data']['dataList'] = Db::query($sql);
        $sql = "SELECT count(count_table.id) as total_record_num  from ( {$sqlAll} ) as count_table";
        //echo $sql;die();
        $count = Db::query($sql);
        //如果之后1页，手动添加分页信息
        $result['data']['pageInfo']['current_page'] = $pageIndex;
        $result['data']['pageInfo']['first_page'] = 1;
        $result['data']['pageInfo']['prev_page'] = $pageIndex - 1;
        $result['data']['pageInfo']['next_page'] = $pageIndex + 1;
        $result['data']['pageInfo']['last_page'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['data']['pageInfo']['total_count'] = $count[0]['total_record_num'];
        $result['data']['pageInfo']['total_page'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['data']['pageInfo']['page_size'] = $pageSize;
        $result['data']['pageInfo']['all_pages'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['errorCode'] = 0;
        $result['errorInfo'] = '查询成功';

        return $result;
    }

    /**
     * 分页获取数据表单信息
     */
    public function pagingOrder($page, $whereStr)
    {
        $page['pageIndex'] ? $pageIndex = $page['pageIndex'] : $pageIndex = 1;
        $page['pageSize'] ? $pageSize = $page['pageSize'] : $pageSize = 10;

        $m = ($pageIndex - 1) * $pageSize;
        $n = $pageSize;

        $where = 'id IS NOT NULL';
        if ($whereStr) {
            $where = "{$whereStr}";
        }

        $querySQL = <<< SQL
SELECT * 
FROM (
    SELECT a.*,c.username
    FROM `{$this->table}` `a` 
        LEFT JOIN `hfztcy_user` `c` ON `a`.`user_id`=`c`.`user_id` 
) AS a
WHERE {$where} 
ORDER BY a.id DESC 
SQL;

        $sql = $querySQL . " LIMIT {$m}, {$n}";
//        echo $sql;die();
        $sqlAll = $querySQL;

        $result['data']['dataList'] = Db::query($sql);
        $sql = "SELECT count(count_table.id) as total_record_num  from ( {$sqlAll} ) as count_table";
//        echo $sql;die();
        $count = Db::query($sql);
        //如果之后1页，手动添加分页信息
        $result['data']['pageInfo']['current_page'] = $pageIndex;
        $result['data']['pageInfo']['first_page'] = 1;
        $result['data']['pageInfo']['prev_page'] = $pageIndex - 1;
        $result['data']['pageInfo']['next_page'] = $pageIndex + 1;
        $result['data']['pageInfo']['last_page'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['data']['pageInfo']['total_count'] = $count[0]['total_record_num'];
        $result['data']['pageInfo']['total_page'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['data']['pageInfo']['page_size'] = $pageSize;
        $result['data']['pageInfo']['all_pages'] = ceil($count[0]['total_record_num'] / $pageSize);
        $result['errorCode'] = 0;
        $result['errorInfo'] = '查询成功';

        return $result;
    }

    /**
     * Info: 订单号生成
     * Date: 2022/6/28 10:49
     */
    public function generateOrderNum()
    {
        $order_num = "ZTCY" . date("YmdHis") . rand(1000, 9999);
        $orderInfo = $this->findOrder(array('code' => $order_num));
        if ($orderInfo['errorCode'] == 0) {
            $this->generateOrderNum();
        } else {
            return $order_num;
        }
    }

    /**
     * Info: 数据验证
     * Date: 2022/6/17 9:55
     */
    public function goCheck($data, $opt = 'add')
    {
        $rule = [
            'user_id' => 'require',
            'amount' => 'require',
            'like_user_id' => 'require',
        ];

        $msg = [
            'user_id.require' => '用户不能为空',
            'amount.require' => '金额不能为空',
            'like_user_id.require' => '被浏览的人不能为空',
        ];

        $scene = [
            'add' => [
                'user_id',
                'amount',
                'like_user_id',
            ],
            'edit' => [
                'user_id',
                'amount',
                'like_user_id',
                'id'
            ],
        ];

        $validate = new Validate($rule, $msg);
        $validate->scene($opt, $scene[$opt]);
        if (!$validate->scene($opt)->check($data)) {
            return Common::errorArray(1, $validate->getError(), false);
        }
        return Common::errorArray(0, '验证通过', true);
    }

    //"2 vip收费模式（单次付费查看视频）
    //（1.1）游客/注册会员/vip会员/推广红娘：直推50%
    //（1.2）专属红娘（直推20%）
    //备注：如果一个用户他是红娘，他吧他的直推下级视频认证会员推广出去，别人看了视频付费则他可以获得50%+20%的分成，如果有其他用户b把他的下级会员分享出去，则他们能分5成，这个专属红娘分2成。
    //（2）市代：5%
    //（3）区县代：5%
    //（4）总部：剩余的是总部的"
    public static function getOrderDistributeByOneYuan($order)//0
    {
        return self::getDistributeUserList($order,0);
    }

    //"1 vip收费模式（月卡/季卡/年卡）
    //（1）市代：5%
    //（2）区县代：5%
    //（3）总部：剩余的是总部的"
    public static function getOrderDistributeByVip($order)//1
    {
        return self::getDistributeUserList($order,1);
    }

    //"3 视频认证收费模式：扣除999元制作费之后剩余的钱按照下述比例分润
    //（1）红娘（直推40%+间推10%）
    //（2）门店：20%
    //（3）市代：5%
    //（4）区县代：5%
    //（4）客服部：5%
    //（5）培训部：5%
    //（6）总部：剩余的是总部的"
    public static function getOrderDistributeByVipAuth($order)//2
    {
        return self::getDistributeUserList($order,2);
    }

    //"4 红娘收费模式：扣除999元制作费之后剩余的钱按照下述比例分润
    //（1）红娘（直推40%+间推10%）
    //（2）市代：5%
    //（3）区县代：5%
    //（3）客服部：5%
    //（4）培训部：5%
    //（5）总部：剩余的是总部的"
    public static function getOrderDistributeByRedMotherAuth($order)//3
    {
        return self::getDistributeUserList($order,3);
    }

    /**
     * Info: 获取分销用户列表
     * Date: 2022/11/18 8:59
     */
    private static function getDistributeUserList($order, $type)
    {
        $user_id = $order['user_id'];
        $data = [];
        $commissionRecordModel = new LibBalanceRecords();
        $adminCommissionRecordModel = new LibAdminBalanceRecords();
        $userModel = new LibUser();
        $adminModel = new LibAdmin();
        $memberModel = new LibMember();
        $storeModel = new LibStore();

        $findUserRes = $memberModel->findMemberToUser($user_id);
        if ($findUserRes['errorCode'] == 0) {
            $memberInfo = $findUserRes['data'];
            $parent_id = $memberInfo['parent_id'];
            $city = !empty($memberInfo['city']) ? $memberInfo['city'] : $memberInfo['acity'];
            $area = !empty($memberInfo['area']) ? $memberInfo['area'] : $memberInfo['aarea'];
            $order_id = $order['id'];
            $store_id = $order['store_id'];

            if ($type == 0) {
                $findOrderOneYuanRes = (new LibOrderOneYuan())->findOrder(['order_id' => $order_id]);
                // 直推用户
                $data['direct_user'] = self::getDirectUser($userModel,$memberModel,$findOrderOneYuanRes);
                // 专属红娘
                $data['private_user'] = self::getPrivateRedMotherUser($userModel,$memberModel,$findOrderOneYuanRes);
                // 门店
                $data['store_user'] = self::getStoreByVideo($userModel,$memberModel,$findOrderOneYuanRes);
            }

            if (in_array($type, array(2, 3))) {
                if ($type == 2) {
                    $findOrderRes = (new LibOrderVipAuth())->findOrder(['order_id' => $order_id]);
                } else {
                    $findOrderRes = (new LibOrderRedMother())->findOrder(['order_id' => $order_id]);
                }
                // 直推用户
                $data['direct_user'] = self::getDirectUserByRedMother($userModel, $memberModel, $findOrderRes);
                // 间推用户
                $data['indirect_user'] = self::getIndirectUserByRedMother($userModel, $memberModel, $findOrderRes);
            }

            if ($type == 2 && $store_id) {
                // 门店
                $res = self::getStore($userModel, $storeModel, $memberModel, $order_id, $store_id);
                $data['store_user'] = $res && isset($res['store']) ? $res['store'] : [];
                $data['store_red_mother'] = $res && isset($res['store_red_mother']) ? $res['store_red_mother'] : [];
                $data['store_parent_user'] = $res && isset($res['store_parent_user']) ? $res['store_parent_user'] : [];
            }

            // 市代
            $data['shi_user'] = self::getShi($adminModel, $adminCommissionRecordModel, $order_id, $city);
            // 区县代
            $data['xian_user'] = self::getXian($adminModel, $adminCommissionRecordModel, $order_id, $area);

            if (in_array($type, array(2, 3))) {
                // 客服部
                $data['customer_user'] = self::getCustomer($adminModel, $adminCommissionRecordModel, $order_id);
                // 培训部
                $data['train_user'] = self::getTrain($adminModel, $adminCommissionRecordModel, $order_id);
            }
        }
        return $data;
    }

    /**
     * Info: 直推用户
     * Date: 2022/11/18 10:04
     */
    private static function getDirectUser($userModel, $memberModel, $findOrderOneYuanRes)
    {
        $data = [];
        if ($findOrderOneYuanRes['errorCode'] == 0) {
            $direct_user_id = $findOrderOneYuanRes['data']['direct_user_id'];
            //$findUserRes = $userModel->findUser(['id' => $direct_user_id]);
            $findMemberRes = $memberModel->findMember(['user_id' => $direct_user_id]);
            $data = [
                'user_id' => $direct_user_id,
                'username' => $findMemberRes['data']['username'],
                'amount' => $findOrderOneYuanRes['data']['zt_commission_money'],
                'describe' => '直推用户',
            ];
        }
        return $data;
    }

    /**
     * Info: 门店
     * Date: 2022/11/18 10:04
     */
    private static function getStoreByVideo($userModel, $memberModel, $findOrderOneYuanRes)
    {
        $data = [];
        if ($findOrderOneYuanRes['errorCode'] == 0) {
            $store_user_id = $findOrderOneYuanRes['data']['store_user_id'];
            //$findUserRes = $userModel->findUser(['id' => $store_user_id]);
            $findMemberRes = $memberModel->findMember(['user_id' => $store_user_id]);
            $data = [
                'user_id' => $store_user_id,
                'username' => $findMemberRes['data']['username'],
                'amount' => $findOrderOneYuanRes['data']['md_commission_money'],
                'describe' => '门店',
            ];
        }
        return $data;
    }

    /**
     * Info: 专属红娘
     * Date: 2022/11/18 10:04
     */
    private static function getPrivateRedMotherUser($userModel,$memberModel, $findOrderOneYuanRes)
    {
        $data = [];
        if ($findOrderOneYuanRes['errorCode'] == 0) {
            $private_red_mother_id = $findOrderOneYuanRes['data']['private_red_mother_id'];
            //$findUserRes = $userModel->findUser(['id' => $private_red_mother_id]);
            $findMemberRes = $memberModel->findMember(['user_id' => $private_red_mother_id]);
            $data = [
                'user_id' => $private_red_mother_id,
                'username' => $findMemberRes['data']['username'],
                'amount' =>$findOrderOneYuanRes['data']['zs_commission_money'],
                'describe' => '专属红娘',
            ];
        }
        return $data;
    }

    /**
     * Info: 直推用户
     * Date: 2022/11/17 17:46
     */
    private static function getDirectUserByRedMother($userModel, $memberModel,$findOrderRes)
    {
        $data = [];
        if ($findOrderRes['errorCode'] == 0) {
            $user_id = $findOrderRes['data']['direct_user_id'];
            //$findUserRes = $userModel->findUser(['id' => $user_id]);
            $findMemberRes = $memberModel->findMember(['user_id' => $user_id]);
            $data = [
                'user_id' => $user_id,
                'username' => $findMemberRes['data']['username'],
                'amount' => $findOrderRes['data']['zt_commission_money'],
                'is_red_mother' => 1,
                'describe' => '直推红娘',
            ];
        }
        return $data;
    }

    /**
     * Info: 间推用户
     * Date: 2022/11/17 17:45
     */
    private static function getIndirectUserByRedMother($userModel, $memberModel,$findOrderRes)
    {
        $data = [];
        if ($findOrderRes['errorCode'] == 0) {
            $user_id = $findOrderRes['data']['indirect_user_id'];
            //$findUserRes = $userModel->findUser(['id' => $user_id]);
            $findMemberRes = $memberModel->findMember(['user_id' => $user_id]);
            $data = [
                'user_id' => $user_id,
                'username' => $findMemberRes['data']['username'],
                'amount' => $findOrderRes['data']['jt_commission_money'],
                'is_red_mother' => 1,
                'describe' => '间推红娘',
            ];
        }
        return $data;
    }

    /**
     * Info: 市代理
     * Date: 2022/11/17 17:42
     */
    private static function getShi($adminModel, $adminCommissionRecordModel, $order_id, $city)
    {
        $data = [];
        if (empty($city)) return $data;
        $findAdminCityRes = $adminModel->findAdmin(['group_id' => 3, 'is_area' => 0, 'city' => $city]);
        if ($findAdminCityRes['errorCode'] == 0) {
            $shi_admin_id = $findAdminCityRes['data']['id'];
            $findCommissionRes = $adminCommissionRecordModel->findCommissionRecords(['order_id' => $order_id, 'user_id' => $shi_admin_id]);
            $amount = 0;
            if ($findCommissionRes['errorCode'] == 0) $amount = $findCommissionRes['data']['amount'];
            $data = [
                'user_id' => $shi_admin_id,
                'username' => $findAdminCityRes['data']['account'],
                'amount' => $amount,
                'describe' => '市代理商',
            ];
        }
        return $data;
    }

    /**
     * Info: 区县代理
     * Date: 2022/11/17 17:40
     */
    private static function getXian($adminModel, $adminCommissionRecordModel, $order_id, $area)
    {
        $data = [];
        if (empty($area)) return $data;
        $findAdminAreaRes = $adminModel->findAdmin(['group_id' => 3, 'is_area' => 1, 'area' => $area]);
        if ($findAdminAreaRes['errorCode'] == 0) {
            $xian_admin_id = $findAdminAreaRes['data']['id'];
            $findCommissionRes = $adminCommissionRecordModel->findCommissionRecords(['order_id' => $order_id, 'user_id' => $xian_admin_id]);
            $amount = 0;
            if ($findCommissionRes['errorCode'] == 0) $amount = $findCommissionRes['data']['amount'];
            $data = [
                'user_id' => $xian_admin_id,
                'username' => $findAdminAreaRes['data']['account'],
                'amount' => $amount,
                'describe' => '区县代理商',
            ];
        }
        return $data;
    }

    /**
     * Info: 客服部
     * Date: 2022/11/17 17:38
     */
    private static function getCustomer($adminModel, $adminCommissionRecordModel, $order_id)
    {
        $data = [];
        $findCustomerRes = $adminModel->findAdmin(['group_id' => 1]);
        if ($findCustomerRes['errorCode'] == 0) {
            $kf_admin_id = $findCustomerRes['data']['id'];
            $findCommissionRes = $adminCommissionRecordModel->findCommissionRecords(['order_id' => $order_id, 'user_id' => $kf_admin_id]);
            $amount = 0;
            if ($findCommissionRes['errorCode'] == 0) $amount = $findCommissionRes['data']['amount'];
            $data = [
                'user_id' => $kf_admin_id,
                'username' => $findCustomerRes['data']['account'],
                'amount' => $amount,
                'describe' => '客服部',
            ];
        }
        return $data;
    }

    /**
     * Info: 培训部
     * Date: 2022/11/17 17:36
     */
    private static function getTrain($adminModel, $adminCommissionRecordModel, $order_id)
    {
        $data = [];
        $findTrainRes = $adminModel->findAdmin(['group_id' => 2]);
        if ($findTrainRes['errorCode'] == 0) {
            $px_admin_id = $findTrainRes['data']['id'];
            $findCommissionRes = $adminCommissionRecordModel->findCommissionRecords(['order_id' => $order_id, 'user_id' => $px_admin_id]);
            $amount = 0;
            if ($findCommissionRes['errorCode'] == 0) $amount = $findCommissionRes['data']['amount'];
            $data = [
                'user_id' => $px_admin_id,
                'username' => $findTrainRes['data']['account'],
                'amount' => $amount,
                'describe' => '培训部',
            ];
        }
        return $data;
    }

    /**
     * Info: 门店
     * Date: 2022/11/18 8:25
     */
    private static function getStore($userModel, $storeModel, $memberModel, $order_id, $store_id)
    {
        $data = [];
        if ($store_id) {
            $findStoreRes = $storeModel->findStore(['id' => $store_id]);
            if ($findStoreRes['errorCode'] == 0)
            {
                $store_user_id = $findStoreRes['data']['uid'];
                $findUserRes = $userModel->findUser(['id' => $store_user_id]);
                if ($findUserRes['errorCode'] == 0)
                {
                    $orderModel = new LibOrderVipAuth();
                    $findOrderRes = $orderModel->findOrder(['order_id' => $order_id]);
                    if ($findOrderRes['errorCode'] == 0)
                    {
                        $store_parent_user_id = $findOrderRes['data']['store_parent_user_id'];
                        if (floatval($findOrderRes['data']['md_give_red_mother_commission_money'])) {
                            $findDirectUserRes = $memberModel->findMember(['user_id' => $findOrderRes['data']['direct_user_id']]);
                            $directUserName = '';
                            if ($findDirectUserRes['errorCode'] == 0) {
                                $directUserInfo = $findDirectUserRes['data'];
                                $directUserName = $directUserInfo['username'];
                            }
                            $data['store_red_mother'] = [
                                'user_id' => $store_user_id,
                                'username' => $directUserName,
                                'amount' => $findOrderRes['data']['md_give_red_mother_commission_money'],
                                'describe' => '门店抽佣直推红娘',
                            ];
                        }
                        if (floatval($findOrderRes['data']['store_parent_user_commission_money'])) {
                            $findDirectUserRes = $memberModel->findMember(['user_id' => $findOrderRes['data']['store_parent_user_id']]);
                            $directUserName = '';
                            if ($findDirectUserRes['errorCode'] == 0) {
                                $directUserInfo = $findDirectUserRes['data'];
                                $directUserName = $directUserInfo['username'];
                            }
                            $data['store_parent_user'] = [
                                'user_id' => $store_parent_user_id,
                                'username' => $directUserName,
                                'amount' => $findOrderRes['data']['store_parent_user_commission_money'],
                                'describe' => '门店上级',
                            ];
                        }
                        if (floatval($findOrderRes['data']['md_commission_money'])) {
                            $data['store'] = [
                                'user_id' => $store_user_id,
                                'username' => $findStoreRes['data']['title'].'('.$findUserRes['data']['nick_name'].')',
                                'amount' => $findOrderRes['data']['md_commission_money'],
                                'describe' => '门店',
                            ];
                        }
                    }
                }
            }
        }
        return $data;
    }
}

?>